import { queryDictList } from '@/services/dict';
import { queryDetails, queryPage, exportAlarm, exportDevice } from './service'
import { download } from '@/utils/xlsx';

export default {
  namespace: 'analyse_check',

  state: {
    list: { data: [] },
    details: [],
    cond: {},
    dict: {}
  },

  effects: {
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *queryPage({ payload }, { call, put }) {
      const { list = [], total = 0 } = yield call(queryPage, payload);
      yield put({ type: 'setPage', payload: { list, total: +total } })
    },
    *queryDetails({ payload }, { call, put }) {
      const response = yield call(queryDetails, payload);
      yield put({ type: 'setDetails', payload: response });
    },
    *exportAlarm({ payload }, { call }) {
      const response = yield call(exportAlarm, payload.id);
      download(payload.name, response)
    },
    *exportDevice({ payload }, { call }) {
      const response = yield call(exportDevice, payload.id);
      download(payload.name, response)
    }
  },

  reducers: {
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    setPage(state, action) {
      return {
        ...state,
        list: action.payload
      }
    },
    setDetails(state, action) {
      return {
        ...state,
        details: action.payload
      }
    }
  }
}
